<?php
namespace app\api\controller\audience;
use app\api\controller\common\ApiBase;
use think\Db;
use think\Model;
use app\api\model\Admin as AdminModel;
use think\Controller; // dengh quxiaoo
class Potential extends ApiBase{
    public function _initialize()
    {
        parent::_initialize();
        $this->admin = session('admin');
    }

    public function getTabName($exhibition_id){
        $result=db('exhibition')->where(['id'=>$exhibition_id])->find();
        $tabName='';
        if($result){
            $tabName=$result['create_person_table'];
        }else{
            return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
        }
        return $tabName;
    }

    /**
     * 私海列表
     * @param member_id,public_id,sale_id
     */
    public function index(){
        $page = $this->request->param('page');
        $page = max(1,intval($page));
        $pagesize = input('pagesize');
        $pagesize = empty($pagesize)?10:$pagesize;//吗默认为10
        $where['pc.back'] = array('eq',2);//退回公海的不显示
        $status = $this->request->param('status');
        $keyword =input('keyword');
        $exhibition_id=$this->request->param('exhibition_id');
        if(empty($exhibition_id)){
            return json(['code'=>0,'msg'=>'展会数据有误']);
        }
        $tabName=$this->getTabName($exhibition_id);
        // and
        if (!empty($keyword)) {
            // 公司名模糊 其他没必要
            $where['pc.company_name'] = array('like', '%' . $keyword . '%');
            // 公司名模糊 其他没必要
//            $where['pc.company_name'] = array('like', '%' . $keyword . '%');
        }
        //当前用户权限
        $role_id = db('admin_role')->where(['admin_id'=>$this->admin['id']])->value('role_id');
        if(!$role_id && $this->admin['is_super']==0){
            return json(['code'=>0,'msg'=>'角色不存在']);
        }
        $is_gl = db('role')->where(['id'=>$role_id])->value('is_gl');
        if($this->admin['is_super'] == 1){
            //不加限制条件
        }else{
            if($is_gl == 1){
                $w['org_id_tree'] = ['like', "%".$this->admin['org_id']."%"];
                //所有所属组织的用户 id
                $adminarr =db('admin')->field('id')->where($w)->select();
                $admin_ids = array_column($adminarr,'id');
                $where['pc.member_id'] = array('in',$admin_ids);
            }else{
                $where['pc.member_id'] = $this->admin['id'];
            }
        }
        if($status||$status==="0"){
            $where['pc.status'] = array('in',$status);
        }
        // if(!empty($this->request->param('project_id'))){
        //     $where['p.project_id'] = $this->request->param('project_id');
        // }

        //查询可优化
        $list = Db::name($tabName)
            ->alias('pc')
            ->field('pc.id,pc.data_from,pc.company_name,pc.create_time,pc.person_id,pc.area_city,pc.info_level,pc.name_cn,pc.company_address,pc.link,pc.contact,pc.category_id,pc.second_category_id,pc.status,pc.back,pc.member_id,a.id as aid,a.realname')
            ->join('admin a','pc.member_id=a.id','left')
            ->where($where)->order('pc.id desc')
            ->limit(($page-1)*$pagesize.','.$pagesize)
            ->select();
        foreach ($list as $k =>&$v){
            //最近联络时间
            $where1['follow_id'] = $v['id'];
            $FollowLog = db('follow_user_log')->where($where1)->order('id desc')->select();
            //查询展位号数据
            $v['followlog'] = $FollowLog?$FollowLog[0]:'';
            unset($FollowLog);
        }
        $total =  Db::name($tabName)
            ->alias('pc')
            ->field('pc.id')
            ->join('admin a','pc.member_id=a.id','left')
            ->where($where)
            ->count();
//        //统计各个状态下的数量
        //$status_count = $this->model->field("status,count('id') as sum")->where(['is_status'=>array('<>',-1)])->group('status')->select();
        return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'count'=>$total]]);
    }


}



